<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
      <child></child>
    </div>
    <script src="vue.js"></script>
    <script>
        var vm = new Vue({
            el:'#app',
            data:{name:'根实例/父组件'},
            components:{
                child:{
                    template:`<div>
                        child
                        <grandson1></grandson1>
                        <grandson2 ref='grandson2'></grandson2>
                        <grandson3></grandson3>
                        <button @click="print">print</button>
                        </div>`,
                    data:()=>({name:'子组件'}),    
                    methods:{
                        print(){
                            console.log('this.$root',this.$root.name);
                            console.log('this.$parent',this.$parent.name);
                            console.log('this',this.name);
                            //永远不要通过索引来获取某个子组件的实例
                            console.log('this.$children[0]',
                            this.$children[0].name);
                            console.log('this.$children[1]',
                            this.$children[1].name);
                            console.log('this.$children[2]',
                            this.$children[2].name);
                            console.log('this.$refs.grandson2',this.$refs.grandson2.name)
                        
                        }
                    },    
                    components:{
                        grandson1:{
                            template:`<div>grandson1</div>`,
                            data:()=>({name:'孙子1'}),    
                        },
                        grandson2:{
                            template:`<div>grandson2</div>`,
                            data:()=>({name:'孙子2'}),    
                        },
                        grandson3:{
                            template:`<div>grandson3</div>`,
                            data:()=>({name:'孙子3'}),    
                        }
                    }
                }
            }
        });
    </script>
</body>
</html>